package com.dev.manager.controller;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.dev.common.base.controller.BaseController;
import com.dev.common.dto.ResponseData;
import com.dev.facade.entity.SyParam;
import com.dev.facade.service.SyParamService;
import com.dev.facade.service.SyUserInfoService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.github.pagehelper.Page;

/**
 * 
 * @Description 变量设置
 * @author Shiwen .Li
 * @date 2016年8月18日 下午9:18:09
 * @version V1.3.1
 */
@Controller
@RequestMapping("variable")
public class VariableControll extends BaseController {

	/**
	 * 系统参数业务
	 */
	@Resource
	protected SyParamService paramService;


	/**
	 * 系统用户 业务实例
	 */
	@Resource
	protected SyUserInfoService userService;
	/**
	 * 变量列表页面
	 * 
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String index() {
		Subject subject = SecurityUtils.getSubject();
		logger.info("当前用户:{}",subject);
		logger.info("变量列表页面");
		return "variable/list";
	}

	/**
	 * 变量分页查询数据
	 * 
	 * @param params
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "data")
	public void data(@RequestBody SyParam params, HttpServletResponse response)
			throws IOException {
		logger.info("分页{},{}", params.getOffset(), params.getLimit());
		Page<SyParam> page =paramService.getListByPage(params) ;
		bootstrapDataTable(response, page);
	}

	/**
	 * 添加或修改系统参数
	 * @param params 系统参数
	 * @return
     */
	@RequestMapping(value = "create",method = RequestMethod.POST)
	@ResponseBody
	public ResponseData create(SyParam params){
		logger.info("添加或修改系统参数:{}",params);
		ResponseData data=new ResponseData();
		int count=0;
		if(params.getId()!=null){
			count=paramService.update(params);
		}else{
			count=paramService.save(params);
		}
		if(count==0){
			data.setMsg("操作失败！");
		}else{
			data.setMsg("操作成功！");
		}
		data.setStatus(count);
		data.setData(count);
		return data;
	}
	/**
	 * 删除系统参数
	 * @param ids 参数id
	 * @return
     */
	@RequestMapping(value = "delete",method = RequestMethod.POST)
	@ResponseBody
	public ResponseData delete(@RequestParam(value = "ids",defaultValue ="",required = true)String ids){
		logger.info("删除系统参数:{}",ids);
		ResponseData data=new ResponseData();
		int count=paramService.delete(ids);
		if(count==0){
			data.setMsg("操作失败！");
		}else{
			data.setMsg("操作成功！");
		}
		data.setStatus(0);
		data.setData(count);
		return data;
	}

}
